/**
 * @(#) PaySummaryProxy.as
 *
 * @date		2010. 11. 01
 * @version		2.0
 * @author		Shin, Yunwoo
 * @client		Service Ace 
 * @module		Compensation  (급여)
 * @description	급여리스트 조회- Proxy
 * 				
 */
package com.skt.common_hr.compensation.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.compensation.CompensationFacade;
	import com.skt.common_hr.compensation.model.vo.PaySummary;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class PaySummaryProxy extends CommonProxy
	{
		public static const NAME:String = "PaySummaryProxy";
		
		public var payList:ArrayCollection = new ArrayCollection()
		public var payTotal:PaySummary = new PaySummary();
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function PaySummaryProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;					// 사번
			param["I_GJAHR"] = year;					// 사번

			loadData("Z_HR_0006", param, loadResult, loadFault);
		}
		



		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		 
		public var sO_DYN:String=""; 
		public function loadResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				payList = parsePayList(event.result.T_RESULT);
				sO_DYN 	= event.result.O_DYN;
				facade.sendNotification(CompensationFacade.LOAD_PAYLIST_COMPLETE, this);
			}
			else {
				payList.removeAll();
				payTotal = null;
				facade.sendNotification(CompensationFacade.LOAD_PAYLIST_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadFault(event:FaultEvent):void
		{
			facade.sendNotification(CompensationFacade.LOAD_PAYLIST_FAILED, this);
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parsePayList(srvTable:ArrayCollection):ArrayCollection
		{
			var coll:ArrayCollection = new ArrayCollection();
			var amt01_total:Number = 0;
			var amt02_total:Number = 0;
			var amt03_total:Number = 0;
			var amt04_total:Number = 0;
			var amt05_total:Number = 0;
			var amt06_total:Number = 0;
			var amt07_total:Number = 0;
			var amt08_total:Number = 0;
			var amt09_total:Number = 0;
			var amt10_total:Number = 0;
			var amt11_total:Number = 0;
			var amt12_total:Number = 0;
			
			for each(var rec:Object in srvTable)
			{
				var vo:PaySummary = new PaySummary();
				if(Variables.company == 'SAC') {
					vo.PAYID = rec.PAYID;
					vo.PAYTY = rec.PAYTY;					
				}

				
				vo.PAYDT = DateUtil.stringToDateTime(rec.PAYDT);
				vo.ACTDT = DateUtil.stringToDateTime(rec.ACTDT);
				vo.OCRTX = rec.OCRTX.toString();
				vo.OCRSN = rec.OCRSN.toString();
				vo.AMT01 = Math.round(rec.AMT01 * 100);
				vo.AMT02 = Math.round(rec.AMT02 * 100);
				vo.AMT03 = Math.round(rec.AMT03 * 100);
				vo.AMT04 = Math.round(rec.AMT04 * 100);
				vo.AMT05 = Math.round(rec.AMT05 * 100);
				vo.AMT06 = Math.round(rec.AMT06 * 100);
				vo.AMT07 = Math.round(rec.AMT07 * 100);
				vo.AMT08 = Math.round(rec.AMT08 * 100);
				vo.AMT09 = Math.round(rec.AMT09 * 100);
				vo.AMT10 = Math.round(rec.AMT10 * 100);
				vo.AMT11 = Math.round(rec.AMT11 * 100);
				vo.AMT12 = Math.round(rec.AMT12 * 100);
				
				amt01_total += vo.AMT01;
				amt02_total += vo.AMT02;
				amt03_total += vo.AMT03;
				amt04_total += vo.AMT04;
				amt05_total += vo.AMT05;
				amt06_total += vo.AMT06;
				amt07_total += vo.AMT07;
				amt08_total += vo.AMT08;
				amt09_total += vo.AMT09;
				amt10_total += vo.AMT10;
				amt11_total += vo.AMT11;
				amt12_total += vo.AMT12;
				
				coll.addItem(vo);
			}
			payTotal = new PaySummary();
			payTotal.AMT01_TOTAL = amt01_total;
			payTotal.AMT02_TOTAL = amt02_total;
			payTotal.AMT03_TOTAL = amt03_total;
			payTotal.AMT04_TOTAL = amt04_total;
			payTotal.AMT05_TOTAL = amt05_total;
			payTotal.AMT06_TOTAL = amt06_total;
			payTotal.AMT07_TOTAL = amt07_total;
			payTotal.AMT08_TOTAL = amt08_total;
			payTotal.AMT09_TOTAL = amt09_total;
			payTotal.AMT10_TOTAL = amt10_total;
			payTotal.AMT11_TOTAL = amt11_total;
			payTotal.AMT12_TOTAL = amt12_total;
			return coll;
		}
	}
}